package com.hengxinguotong.zhihuichengjian.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.hengxinguotong.zhihuichengjian.R;
import com.hengxinguotong.zhihuichengjian.bean.User;
import com.hengxinguotong.zhihuichengjian.ui.SearchNoticedPersonActivity;
import com.hengxinguotong.zhihuichengjian.ui.SearchPersonActivity;
import com.hengxinguotong.zhihuichengjian.utils.Utils;
import com.hengxinguotong.zhihuichengjian.widget.font.HXTextView;
import com.hengxinguotong.zhihuichengjian.widget.view.CircleImageView;
import com.orhanobut.logger.Logger;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by liux on 2016/7/11.
 */
public class AddNoticedPersonAdapter extends BaseAdapter {

    private final Context context;
    private List<User> userInfos;
    private SearchNoticedPersonActivity mSearchPersonActivity;

    public AddNoticedPersonAdapter(List<User> userInfos, Context context) {
        this.context = context;
        this.userInfos = userInfos;
        mSearchPersonActivity = (SearchNoticedPersonActivity) context;
    }

    public void setDatas(List<User> userInfos){
        this.userInfos = userInfos;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return userInfos.size();
    }

    @Override
    public Object getItem(int position) {
        return userInfos.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null || convertView.getTag() == null) {
            convertView = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.item_addgroup, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
            refreshViewHolder(holder);
        }
        User user = userInfos.get(position);
        String headPortrait = user.getHeadPortrait();
        if(!Utils.isEmpty(headPortrait)){
            headPortrait = headPortrait.substring(0, headPortrait.lastIndexOf(".")) + "_100x100" + headPortrait.substring(headPortrait.lastIndexOf("."));
        }
        Glide.with(context).
                load(headPortrait)
                .placeholder(R.mipmap.icon_avater_defalut)
                .dontAnimate()
                .error(R.mipmap.icon_avater_defalut)
                .into(holder.itemAvatar);
//        Glide.with(context).load(Utils.SERVER_PIC + user.getHeadPortrait()).error(R.mipmap.icon_avater_defalut).into(holder.itemAvatar);
        if (user.isInUsers(mSearchPersonActivity.getSelectUsers())) {
            holder.checkbox.setImageResource(R.mipmap.circle_choice_ligt);
        } else {
            holder.checkbox.setImageResource(R.mipmap.circle_choice);
        }
        holder.nameTv.setText(user.getName());
        holder.mobileTv.setText(user.getMobile());
        holder.teamTv.setVisibility(View.GONE);
        holder.companyTv.setVisibility(View.GONE);
        holder.labelTv.setVisibility(View.GONE);
        if (user.getTeamName() != null) {
            holder.teamTv.setVisibility(View.VISIBLE);
            holder.teamTv.setText(user.getTeamName());
        }
        if (user.getProjectCompanyName() != null) {
            holder.companyTv.setVisibility(View.VISIBLE);
            holder.companyTv.setText(user.getProjectCompanyName());
        }
        return convertView;
    }

    @Override
    public boolean isEnabled(int position) {
        boolean isEnabled = super.isEnabled(position);
//        for(int i=0; i<indexs.size(); i++){
//            if(position == indexs.get(i)){
//                return false;
//            }
//        }
        return isEnabled;
    }

    private void refreshViewHolder(ViewHolder viewHolder) {
        viewHolder.checkbox.setImageResource(R.mipmap.circle_choice);
        viewHolder.itemAvatar.setImageResource(R.mipmap.detail_loading);
    }

    static class ViewHolder {
        @BindView(R.id.item_avatar)
        CircleImageView itemAvatar;
        @BindView(R.id.name_tv)
        HXTextView nameTv;
        @BindView(R.id.team_tv)
        HXTextView teamTv;
        @BindView(R.id.company_tv)
        HXTextView companyTv;
        @BindView(R.id.label_tv)
        HXTextView labelTv;
        @BindView(R.id.mobile_tv)
        HXTextView mobileTv;
        @BindView(R.id.checkbox)
        ImageView checkbox;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}

